<?php
/**
 *
 * 员工配备 模型
 * author：Rogermmg
 **/
class PeibeiModel extends Model{
	
	//添加
	public function add($data){
		$tab=M('Peibei');
		$dc['user_id']=$data['user_id'];
		$dc['status']='1';
		$dcs=$tab->where($dc)->find();
		
		if($dcs!=NULL){
			return FALSE;
		}else{
			$addp=$tab->add($data);
			return $addp;
		}
	}
	
	//查询单信息
	public function view($uid){
		$tab=M('Peibei');
		$cons['user_id']=$uid;
		$cons['status']='1';
		$dcs=$tab->where($cons)->find();
		$peibei=json_decode($dcs['peibei']);
		$dcs['peibei']=$peibei;
		return $dcs;
	}
	
	//编辑信息显示
	public function show($pid){
		$tab=M('Peibei');
		$cons['pid']=$pid;
		$dcs=$tab->where($cons)->find();
		$peibei=json_decode($dcs['peibei']);
		$dcs['peibei']=$peibei;
		return $dcs;
	}
	
	public function uidget($uid){
		$m=M('peibei');
		$peibei=$m->where(array("user_id"=>$uid))->find();
		return $peibei;
	}
	
	//列表
	public function Gpage($p,$status,$did=''){
		$user=M("User");
		
		$where['status']=$status;
		if($did){
			$where['department_id']=$did;
		}
		
		import('@.ORG.Page');
		$count = $user->where($where)->count();
		$Page = new Page($count,10);
		$show  = $Page->show();
		$punchlist = $user->where($where)->order('reg_time desc')->page($p.',10')->select();
		
		foreach($punchlist as $k=>$vo){
			$plist[$k] = $this->gpei($vo["user_id"]);
			if($plist[$k]){
				$p[$k]=$plist[$k];
			}
			$plist[$k]["user_id"]=$vo["user_id"];
			$plist[$k]['creattime']=$vo['reg_time'];
		}
		
		return array('page'=>$show ,'list'=>$plist);
	}
	//查询单个信息
	function gpei($uid){
		$cons['user_id']=$uid;
		$dcs=M('Peibei')->where($cons)->find();
//		$peibei=json_decode($dcs['peibei'],TRUE);
//		$dcs['peibei']=$peibei;
		return $dcs;
	}
	
	//读取用户信息,返回用户名
	public function Get_user($uid){
		$guser=M('User');
		$cons['status']=1;
		$cons['user_id']=$uid;
		$user=$guser->where($cons)->select();
		
		return $user[0]["name"];
	}
	
	//更新
	public function update($data,$status){
		$m=M('Peibei');
		$upd=$m->where($status)->save($data);
		return $upd;
	}
	
	//删除
	public function delete($pid){
		$m=M('Peibei');
		$status['pid']=$pid;
		$data['status']=0;
		$upd=$m->where($status)->save($data);
		return $upd;
	}

	//检查
	public function check($uid){
		$tab=M('Peibei');
		$cons['user_id']=$uid;
		$cons['status']=1;
		$dcs=$tab->where($cons)->find();
		if($dcs==NULL){
			return FALSE;
		}else{
			return $dcs;
		}
	}

	function getDepartmentList($department_id=0,$string='',$me=0,$notin=0) {
				$array = array();
				$first = empty($department_id) ? '' : $string;
				if($me==1 && $department_id != 0){
					$me = M('department')->where('department_id = %d',$department_id)->find();
					$array[$me['department_id']] = array('department_id'=>$me['department_id'],'name'=>$first.$me['name']);
				}
				$rows = M('department')->where('parent_id = %d',$department_id)->select();
				foreach($rows as $v){
					if($v['department_id'] != $notin){
						$array[$v['department_id']] = array('department_id'=>$v['department_id'],'name'=>$first.$v['name']);
						$str = !empty($string) ? "&nbsp;&nbsp;&nbsp;".$string : '';
						$array = $array + $this->getDepartmentList($v['department_id'],$str,0,$notin);
					}
				}
				return $array;
			}

	
}
?>